var newsCount = 0;
var type = "";
$(function(){
    'use strict';
    //默认显示最新动态
    var newsType = getUrlParms("type");
    newsType = isNull(newsType)?"dynamic":newsType;
    type = newsType;
    getAllNewsByType(newsType);
    layui.use(['element','layer'], function(){
        var $ = layui.jquery
            ,element = layui.element //Tab的切换功能，切换事件监听等，需要依赖element模块
            ,layer = layui.layer; //弹层
            //切换到指定Tab项
        element.tabChange('news-type', type); //切换到相应id类型
        element.on('tab(news-type)', function(data){
            newsType = $(this).attr("lay-id");
            if(isNull(newsType)){
                layer.msg("未获取到要显示的内容,请稍后重试");
                return;
            }else{
                type = newsType;
                getAllNewsByType(newsType);
            }
        });
    });
    //加入单击显示内容事件
    $("body").on('click', '.livelihood li,.policy li,.affairs li', function() {
        window.open("../detail/index.html?id="+$(this).attr("itemid"));
    });
});

/**
 * 根据新闻类型获取所有新闻
 */
function getAllNewsByType(newsType,offset,rows){
    if(isNull(newsType)){
        return;
    }
    offset = isNull(offset)? 0 : offset;
    rows = isNull(rows)? 20 : rows;
    $.ajax({
        url : "/CivilAffairs/news/getAllNewByType",
        type : "post",
        async : false,
        data:{"newsType":newsType,"offset":offset,"rows":rows},
        dataType : "json",
        success : function(result) {
            if("0000"==result.status){
                var data = result.data;
                showNews(data.data,newsType);
                var count = data.count;
                //需要重新加载分页
                if(count != newsCount || newsType != type){
                    newsCount=count;
                    type = newsType;
                    layui.use('laypage',function(){
                        var $ = layui.jquery
                            ,laypage = layui.laypage;
                        //分页组件
                        laypage.render({
                            elem: 'news-page'
                            ,count: count //数据总数，从服务端得到
                            ,layout: ['count', 'prev', 'page', 'next', 'limit', 'skip']
                            ,jump: function(obj, first){
                                //obj包含了当前分页的所有参数，比如：
                                console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
                                console.log(obj.limit); //得到每页显示的条数

                                //首次不执行
                                if(!first){
                                    //do something
                                    console.log("not first");
                                    getAllNewsByType(newsType,obj.curr,obj.limit);
                                }
                            }
                        });
                    });
                }

            }else{
                $("#"+newsType+"-item").html(result.detail);
            }
        },
        error : function() {
            alert("系统异常,请稍后重试");
        }
    });
}

/**
 * 显示新闻内容
 * @param news
 */
function showNews(data,type){
    var newsTpl1 = "<ul class=\"news-content-lsit livelihood\">\r\n\t\r\n\t {{#  layui.each(d.newsData, function(index, item){ }}\r\n\t\t{{#if(index >= 51){ }}\r\n\t\t{{# }else{}}\r\n\t\t\t<li class=\"clearfix\" itemid='{{item.id}}'><a href=\"javascript:;\" ><i class=\"fa fa-list-alt\"></i><span class=\"title\">{{item.title}}</span><i class=\"time\">{{item.time}}</i></a></li>\r\n\t\t{{#} }}\r\n\t{{#  }); }}\r\n</ul>";
    layui.use(['jquery','laytpl'],function(){
        var $ = layui.$;//使用jquery
        var laytpl = layui.laytpl;
        laytpl(newsTpl1).render(data, function(html) {
            $("."+type+"-item").html(html);
        });
    });
}